<template>
  <base-breadcrumb :title="titleName">
    <a-spin :spinning="loading">
      <a-card :bordered="false">
        <title-name title="基本信息" />
        <custom-form
          ref="customForm"
          :isEmit="false"
          :form="form"
          :edit-model="'E'"
          :form-list="formList"
          :rules="rules"
          is-validate
          @selectOrg="selectOrg"
        />
      </a-card>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div class="btn_box page-btn-right-top">
      <a-button :disabled="loading" :loading="subloading" v-if="editable" type="primary" class="button_right" @click="handleSubmit(1)"
        >提交</a-button
      >
      <a-button @click="$close">关闭</a-button>
    </div>
  </base-breadcrumb>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import BaseInputNumber from '@/components/BaseInputNumber'
import BSelect from '@/components/BComponents/BSelect'
import {
  getSelectListByType,
  delSelectListByType
} from '@/api/safetySupervision/common.js'
import {
  getContractorManager,
  addSubcontractor,
  updateSubcontractor,
  getSubcontractorDetail
} from '@/api/safetySupervision/safetySubcontractor/safetySubcontractor.js'
export default {
  name: 'a' + Date.now(),
  components: {},
  data() {
    return {
      loading: false,
      subloading: false,
      yearsOpen: false,
      form: {
        projectId:null
      },
      rules: {
        subcontractorId: [{ required: true, message: '请选择分包单位', trigger: 'change' }],
        nameId: [{required: true,message: '请选择姓名',trigger: 'change'}],
        postCertificate: [{required: true, message: '请输入岗位资格证', trigger: 'blur'}],
        birthday: [{required: true,message: '请选择出生年月',trigger: 'change'}],
        education: [{required: true,message: '请选择学历',trigger: 'change'}],
        beginWorkTime: [{required: true,message: '请选择从事本项工作的开始时间',trigger: 'change'}],
      }, // 校验规则
    }
  },
  computed: {
    titleName() {
      return {
        add: '分包安全员台账新增',
        edit: '分包安全员台账编辑',
      }[this.type] || '分包安全员台账'
    },
    type() {
      return this.$route.query.type
    },
    editable() {
      return this.type == 'add' || this.type == 'edit'
    },
    formList() {
      return [
        {
          label: '所属单位',
          type: 'orgTree',
          keyName: 'projectId',
          rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
          span: 16,
          isAutoSelectFirst:true,
          isNeedAuth:true,
          checkStrictly: true,
          disabled:true,
          statusSet:[1,2,3,4,5,6],
          isQueryProjecDetail: false,
        },
        //       {
        //   type: SelectSubCompany, //'a-select',
        //   keyName: 'branchCompanyId',
        //   label: '所属分公司',
        //   disabled: true,
        //   attrs: {
        //     isDefaultSelected: this.type == 'add',
        //     autoSelected: this.type == 'add'
        //   },
        // },
        // {
        //   type: SelectProject,
        //   keyName: 'projectId',
        //   viewName: 'projectName',
        //   label: '所属项目',
        //   disabled: true,
        //   attrs: {
        //     isClear: this.type == 'add',
        //     autoSelected: this.type == 'add', //this.editable,
        //     isEdit: this.type !== 'add',
        //     keyValue: 'branchCompanyId',
        //     form: this.form,
        //   },
        // },
        {
          type: SelectSubcontractor,
          keyName: 'subcontractorId',
          viewName: 'subcontractorName',
          label: '分包单位',
          disabled: (!this.editable),
          attrs: {
            isClear: this.type == 'add',
            autoSelected: this.type == 'add', //this.editable,
            isEdit: this.type !== 'add',
            keyValue: 'projectId',
            form: this.form,
          },
        },
        {
          type: BSelect,
          keyName: 'nameId',
          viewName: 'name',
          disabled: (!this.editable),
          label: '姓名',
          attrs: {
            form: this.form,
            keyValue: 'subcontractorId',
            request: this.getNameList
          },
          selectChange: (e) => {
            this.form.sexId = e.sex;
            this.form.name = e.name;
            this.form.telephone = e.phone;
            this.form.beginProjectTime = this.$dateFormat(e.enterDate, 'M');
            this.form.endProjectTime = e.exitDate;
          }
        },
        {
          type: BSelect,
          keyName: 'sexId',
          viewName: 'sex',
          disabled: true,
          label: '性别',
          attrs: {
            form: this.form,
            list: [
              { name: '男', id: 1 },
              { name: '女', id: 0 },
            ]
          },
        },
        {
          keyName: 'postCertificate',
          type: 'a-input',
          disabled: (!this.editable),
          label: '岗位资格证',
        },
        {
          keyName: 'birthday',
          type: 'a-month-picker',
          label: '出生年月',
          disabled: (!this.editable),
          attrs: {
            valueFormat: 'YYYY-MM',
            format: 'YYYY-MM'
          }
        },
        {
          keyName: 'telephone',
          type: 'a-input',
          disabled: true,
          label: '电话',
        },
        {
          type: BSelect,
          keyName: 'education',
          viewName: 'educationName',
          disabled: (!this.editable),
          label: '学历',
          attrs: {
            form: this.form,
            // (小学、初中、高中、专科、本科、研究生)
            list: [
              { name: '小学', id: 0 },
              { name: '初中', id: 1 },
              { name: '高中', id: 2 },
              { name: '专科', id: 3 },
              { name: '本科', id: 4 },
              { name: '研究生', id: 5 },
            ]
          },
          selectChange: (e) => {
            // console.log(e)
          }
        },
        {
          keyName: 'beginWorkTime',
          type: 'a-date-picker',
          label: '从事本项工作的开始时间',
          disabled: (!this.editable),
          attrs: {
            open: this.yearsOpen,
            mode: 'year',
            valueFormat: 'YYYY',
            format: 'YYYY'
          },
          openChange: (e) => {
            this.yearsOpen = e
          },
          panelChange: (value, mode) => {
            this.form.beginWorkTime = (value && value.format && value.format('YYYY')) || value || null
            this.yearsOpen = false
          }
        },
        {
          keyName: 'beginProjectTime',
          type: 'a-month-picker',
          label: '调入项目时间',
          disabled: true,
          attrs: {
            valueFormat: 'YYYY-MM',
            format: 'YYYY-MM'
          }
        },
        {
          keyName: 'endProjectTime',
          type: 'a-input',
          disabled: true,
          placeholder: ' ',
          label: '调出项目时间',
        },
      ]
    },
  },
  mounted() {
    if (this.type !== 'add') {
      this.getDetail()
    }else{
      this.form.projectId = this.$userInfo().currentPost.projectId;
    }
  },
  methods: {
    selectOrg(val) {
      if(val.length > 1) {
        this.form.branchCompanyId = val[val.length - 2].orgId
      }
    },
    getDetail() {
      getSubcontractorDetail({id: this.$route.query.id}).then((res) => {
        if (res.code == 200) {
          let data = res.data;
          this.$set(this.form,"id", data.id);
          this.$set(this.form,"branchCompanyId", data.branchCompanyId);
          this.$set(this.form,"projectId", data.projectId);
          this.$set(this.form,"postCertificate", data.postCertificate);
          this.$set(this.form,"subcontractorId", data.subcontractorId);
          this.$set(this.form,"name", data.name);
          this.$set(this.form,"nameId", data.nameId);
          this.$set(this.form,"sexId", data.sexId);
          this.$set(this.form,"birthday", data.birthday);
          this.$set(this.form,"telephone", data.telephone);
          this.$set(this.form,"education", data.education);
          this.$set(this.form,"beginWorkTime", data.beginWorkTime);
          this.$set(this.form,"beginProjectTime", data.beginProjectTime);
          this.$set(this.form,"endProjectTime", data.endProjectTime);
          // console.log(this.form)
        }
      })
    },
    getNameList() {
      let param = {
        needCount: false,
        pageNo: 0,
        pageSize: 1000,
        query: {
          subContractorIds: [this.form.subcontractorId]
        }
      }
      return getContractorManager(param).then((res) => {
        // let dataList = [];
        // res.data.forEach((item, index) => {
        //   if (!dataList.find(itemc => item.employeeId == itemc.employeeId)) {
        //     dataList.push(item)
        //   }
        // })
        return res
      }).catch(() => {
        return { code: 200, data: [] }
      })
    },
    handleSubmit() {
      this.$refs.customForm.validate((valid) => {
        if (valid) {
          const params = { ...this.form }

          const api = this.type == 'add' ? addSubcontractor : updateSubcontractor
          const msg = this.type == 'add' ? '添加' : '编辑'
          console.log(params)
          this.subloading = true
          api(params).then((res) => {
            this.subloading = false
            if (res.code == 200) {
              this.$message.success(msg+'成功')
              this.$close()
            }
          }).catch(() => {
            this.subloading = false
          })
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
